﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Net.Mail;
using System.Net;
using System.Data.SqlClient;

namespace AutoMail
{
    public partial class MidasServiceAautoMail : ServiceBase
    {
        #region constructor
        public MidasServiceAautoMail()
        {
            InitializeComponent();

            if (!System.Diagnostics.EventLog.SourceExists("MailSend"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                "MailSend", "AutoMailLog");
            }

            eventLog1.Source = "MailSend";
            eventLog1.Log = "AutoMailLog";
        }
        #endregion

        #region start
        protected override void OnStart(string[] args)
        {
            eventLog1.WriteEntry("In OnStart --- Sending Mail to");
            System.Timers.Timer time = new System.Timers.Timer();
            time.Start();
            time.Interval = 30000; //Gui di cu moi 5 phut
            time.Elapsed += time_elapsed;
        }
        #endregion

        #region stop
        protected override void OnStop()
        {
        }
        #endregion

        private void time_elapsed(object sender, ElapsedEventArgs e)
        {
            #region information auto mail
            string mailServer = "smtp.gmail.com";
            int port = 587;
            string mailFrom = "midasfpt@gmail.com";//dia chi mail ngươi gui 
            string passMailFrom = "midas123$%^";  //pass mail gui 
            string mailTo = string.Empty;//address to send 
            string body = "Happy Birthday to you !";
            string subject = "Happy Birthday";
            #endregion

            

            try
            {              
                string _subject = string.Empty;
                _subject = subject+"Hệ thống chăm sóc khách hàng Midas";
                 mailTo = "bachdx00487@gmail.com";
                string _mailTo = string.Empty;
               
                    SendEmail(mailServer, port, mailFrom, passMailFrom, mailTo, _subject, body);
                    
                
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry("Error " + ex.Message.ToString());
            } 
           
        }

        #region send mail
        private bool SendEmail(string mailServer, int port,
                               string mailFrom, string passMailFrom,
                               string mailTo, string strSubject, string strBody)
        {
            bool flag = false;
            MailMessage mailMsg = new MailMessage();   

            try
            {
                SmtpClient smtpClient = new SmtpClient(mailServer, port);
                smtpClient.Credentials = new NetworkCredential(mailFrom, passMailFrom);
                smtpClient.EnableSsl = true;

                mailMsg.From = new MailAddress(mailFrom);
                mailMsg.Subject = strSubject;
                mailMsg.Body = strBody;
                mailMsg.To.Add(mailTo);//add mail 
                mailMsg.To.Add("thuybot@gmail.com");
                mailMsg.To.Add("bachdx00487@fpt.edu.vn");                
                smtpClient.Send(mailMsg);

                flag = true;
                eventLog1.WriteEntry("Gửi mail thành cống");
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry("Lỗi: " + ex.Message.ToString());
            }
            finally
            {
                mailMsg = null;
            }

            return flag;
        }
        #endregion

        private void eventLog1_EntryWritten(object sender, EntryWrittenEventArgs e)
        {

        }
    }
}
